<template>
	<view>
		<view class="editaddress">
			<view class="content">
				<view class="row">
					<view class="nominal">收货人</view>
					<view class="input"><input placeholder="请输入收货人姓名" v-model="addressData.name" type="text" />
					</view>
				</view>
				<view class="row">
					<view class="nominal">电话号码</view>
					<view class="input"><input placeholder="请输入收货人电话号码" v-model="addressData.phone" type="number"
							maxlength="11" /></view>
				</view>
				<view class="row" style="display: flex;">
					<view class="nominal">所在地区</view>
					<view class="">
						<uni-list>
							<uni-list-item :right-text="addressStr" show-arrow
								@click.native="addressPopup=true"></uni-list-item>
						</uni-list>
					</view>
				</view>
				<view class="row">
					<view class="nominal">详细地址</view>
					<view class="input"><textarea style="font-size: 28upx;" v-model="addressData.moreAddres"
							auto-height="true" placeholder="输入详细地址" v-if="show == false"></textarea></view>
				</view>
				<view class="row">
					<view class="nominal" style="color: 666;margin-top: 10upx;">设为默认地址</view>
					<view class="input switch">
						<switch :color="colors" style="transform:scale(0.8)" @change="switAnge" :checked="checked">
						</switch>
					</view>
				</view>
			</view>
			<view class="save" @click="addressAdd()" v-if="isadd">
				<view class="btn" :style="'background:' + colors">添加地址</view>
			</view>
			<view class="save" @click="addressModify()" v-if="!isadd">
				<view class="btn" :style="'background:' + colors">修改地址</view>
			</view>
			<sx-address-picker v-model="addressPopup" @confirm="addressConfirm($event, 'formData')" />
			<!-- 省市区选择 province city area初始省市区设置 show:是否显示  @sureSelectArea：确认事件 @hideShow：隐藏事件-->
			<!-- <cc-selectDity :province="province" :city="city" :area="area" :show="show" @changeClick="changeClick"
                @sureSelectArea="onsetCity" @hideShow="onhideShow"></cc-selectDity> -->
			<!-- <ehPicker @conceal="conceal" v-if="popup" /> -->
		</view>
	</view>
</template>

<script>
	// import ehPicker from '@/components/erha-picker/erha-picker.vue';
	export default {
		components: {
			// ehPicker
		},
		data() {
			return {
				popup: false,
				checked: false,
				colors: '#ff6600',
				show: false,
				province: "",
				city: "",
				area: "",
				areaCode: "",
				addressData: {
					name: '',
					phone: '',
					address: '',
					moreAddres: '',
					isdefult: 0
				},
				addressPopup: false,
				formData: {
					country: '',
					province: '',
					city: '',
					area: '',
					street: ''
				},
				debugger: 0,
				addressType: '', //分类 1中国大陆 2港澳台 3海外
				isShow: true,
				isadd: true,
				location: {} ,//
				id:null,
			};
		},
		computed: {
			addressStr() {
				let {
					country,
					province,
					city,
					area,
					street
				} = this.formData
				console.log(this.formData, '我的');
				console.log(this.addressType,'aaaaaaaaaaaa');
				if (this.addressType == 1) country = '中国大陆'
				if (this.addressType == 2) country = '港澳台'
				if (this.addressType == 3) country = '海外'
				return country ? `${country || ''} ${province ||''} ${city ||''} ${area || ''} ${street || ''}` :
					'省 市 区 街道'
			},
		},
		onLoad(option) {
			if (option.id) {
				this.id = option.id
				this.isadd = false;
				uni.setNavigationBarTitle({
					title: '修改地址'
				});
				this.getAddress(option.id)
			}
		},
		methods: {

			getAddress(id) {
				this.$api.getShipments({
					id: id
				}).then((res) => {
					console.log(res, 'res');
					if (res.code == 1) {
						this.location = res.data;
						this.addressData.name = res.data.name;
						this.addressData.phone = res.data.phone;
						this.addressType = res.data.address_type
						// 更新 formData 的字段
						this.formData = {
							country: res.data.country || '',
							province: res.data.province || '',
							city: res.data.city || '',
							area: res.data.area || '',
							street: res.data.street || '',
						}
						this.addressData.moreAddres = res.data.address;
						this.seruer();
					}
				})
			},
			seruer() {
				if (this.location.type == 1) {
					this.checked = true
					this.debugger = 1
				} else {
					this.checked = false
					this.debugger = 0
				}
			},
			addressConfirm(address, key) {
				console.log(1,'22')
                 const [country, province, city, area, street] = address
                if(typeof address == 'string'){
					this.addressType = address == '港澳台' ? 2 : 3
					this[key].country = address
					this[key].province = ''
					this[key].city = ''
					this[key].area = ''
					this[key].street = ''
					return
				};
				// const [country, province, city, area, street] = address
				this.address = province ? address.filter((v, i) => i > 0) : address;
				if (address.length === 5) {
					console.log(323323223);
					this[key].country = country.name
					this.addressType = 1
					this[key].province = province.name
					this[key].city = city.name
					this[key].area = area.name
					this[key].street = street.name
				} else {
					this[key].country = country.name
					this.addressType = country.name == '港澳台' ? 2 : 3
					this[key].province = undefined
					this[key].city = undefined
					this[key].area = undefined
					this[key].street = undefined
				}

			},
            //修改地址
			addressModify(){
				switch (this.formData.country) {
					case '中国大陆':
						this.addressType = 1
						break;
					case '港澳台':
						this.addressType = 2
						break;
					case '海外':
						this.addressType = 3
						break;
				}
				this.$api.getReceiving({
					address_type: this.addressType,
					name: this.addressData.name,
					phone: this.addressData.phone,
					// province:this.formData.country=='中国大陆'?this.formData.province:this.formData.country,
					province: this.formData.province,
					city: this.formData.city,
					area: this.formData.area,
					street: this.formData.street,
					address: this.addressData.moreAddres,
					type: this.debugger,
					id:this.id
					
				}).then(res=>{
					console.log(res, 'res')
					if (res.code == 1) {
						uni.showToast({
							title: res.info,
							icon: "none"
						})
						// uni.$emit('refresh')
						setTimeout(() => {
							uni.navigateBack()
						}, 2000)
					}
				})
			},
			openPicker() {
				console.log('执行打开地址选择器')
				this.popup = true
			},
			// conceal(param) {
			//            // 获取到传过来的 省 市 区 县数据
			//          console.log(param,'省 市 区 县数据');
			// 		 this.addressData.address = param.province + param.city + param.area + param.street
			//          this.popup = false

			//        },   
			// changeClick(value, value2, value3, value4) {

			//     console.log('地址选择器 = ' + value + value2 + value3 + value4);

			//     this.province = value;
			//     this.city = value2;
			//     this.area = value3;
			//     this.areaCode = value4;
			// },
			// onhideShow() {
			//     this.show = false
			//     console.log('执行了关闭地址选择器')
			// },
			// //选中省市区
			// onsetCity(e) {
			//     let data = e.detail.target.dataset;
			//     let address = data.province + data.city + data.area;
			//     this.show = false
			//     this.addressData.address = address
			// },
			//添加地址
			addressAdd() {
				try {
					var reg_user = /^[\u4e00-\u9fa5]{2,4}$/; //2-4个中文字符正则
					var reg_Bubble = /[\u4E00-\u9FA5]/g; //不能输入中文
					var reg_tel =
						/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/; //11位手机号码正则
					if (this.addressData.name == "") {
						uni.showToast({
							title: '请输入您的真实姓名',
							icon: "none"
						})
						return false;
					} else if (!reg_user.test(this.addressData.name)) {
						uni.showToast({
							title: '姓名只能为2-4中文!',
							icon: "none"
						})
						return false;
					}
					if (this.addressData.phone == "") {
						uni.showToast({
							title: '请输入您的手机号',
							icon: "none"
						})
						return false;
					} else if (!reg_tel.test(this.addressData.phone)) {
						uni.showToast({
							title: '请正确填写您的手机号码!',
							icon: "none"
						})
						return false;
					} else if (this.formData.country == "") {
						uni.showToast({
							title: '请选择所在地区',
							icon: "none"
						})
						return false;
					} else if (this.addressData.moreAddres == "") {
						uni.showToast({
							title: '请填写详细地址',
							icon: "none"
						})
						return false;
					}

					// return
					switch (this.formData.country) {
						case '中国大陆':
							this.addressType = 1
							break;
						case '港澳台':
							this.addressType = 2
							break;
						case '海外':
							this.addressType = 3
							break;
					}
					this.$api.getBindReceiving({
						address_type: this.addressType,
						name: this.addressData.name,
						phone: this.addressData.phone,
						// province:this.formData.country=='中国大陆'?this.formData.province:this.formData.country,
						province: this.formData.province,
						city: this.formData.city,
						area: this.formData.area,
						street: this.formData.street,
						address: this.addressData.moreAddres,
						type: this.debugger
					}).then(res => {
						console.log(res, 'res')
						if (res.code == 1) {
							uni.showToast({
								title: res.info,
								icon: "none"
							})
							// uni.$emit('refresh')
							setTimeout(() => {
								uni.navigateBack()
							}, 2000)
						}
					})
				} catch (e) {
					uni.$u.toast(e);
				}
			},
			switAnge(e) {
				if (e.detail.value == false) {
					this.debugger = 0
				} else if (e.detail.value == true) {
					this.debugger = 1
				}
				console.log(this.debugger)
			},

		}
	};
</script>
<style lang="scss" scoped>
	.save {
		position: fixed;
		bottom: 0;
		width: 100%;
		height: 120upx;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.save view {
		display: flex;
	}

	.save .btn {
		box-shadow: 0upx 5upx 10upx rgba(0, 0, 0, 0.4);
		width: 70%;
		height: 80upx;
		border-radius: 80upx;
		background-color: #f23a3a;
		color: #fff;
		justify-content: center;
		align-items: center;
		font-size: 30upx;
		margin-bottom: 60px;
	}

	.save .btn .icon {
		height: 80upx;
		color: #fff;
		font-size: 30upx;
		justify-content: center;
		align-items: center;
	}

	.content {
		display: flex;
		flex-wrap: wrap;
		margin-top: 40upx;
	}

	.content view {
		display: flex;
	}

	.content .row {
		width: 92%;
		margin: 0 4%;
		border-bottom: solid 1upx #eee;
	}

	.content .row .nominal {
		width: 30%;
		height: 80upx;
		font-size: 28upx;
		font-family: Droid Sans Fallback;
		font-weight: 400;
		color: rgba(51, 51, 51, 1);
		align-items: center;
	}

	.content .row .input {
		width: 70%;
		padding: 20upx 0;
		align-items: center;
		font-size: 28upx;

	}

	.content .row .input input {
		font-size: 28upx;
		color: #333333;
	}

	.content .row .switch {
		justify-content: flex-end;
	}

	.content .row .input textarea {
		min-height: 40upx;
		line-height: 40upx;
	}

	.content .del_box {
		width: 100%;
		display: block;
		overflow: hidden;
		margin-right: 30upx;
	}

	.content .del {
		width: 240upx;
		height: 80upx;
		float: right;
		justify-content: center;
		align-items: center;
		font-size: 28upx;
		color: #F23A3A;
		margin: 0 auto;
		margin-top: 50upx;
		border-radius: 38upx;
		background-color: rgba(255, 0, 0, 0.05);
		border-bottom: solid 1upx #eee;
	}

	// .selectcity input {
	//     width: 90%;
	// }

	// .selectcity image {
	//     width: 40upx;
	//     height: 40upx;
	//     float: right;
	// }
</style>